﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public delegate void MyEventHandlerCtlNavigation(object sender, int iValue);

public partial class UserControls_ctlMembers : System.Web.UI.UserControl
{

    public event MyEventHandlerCtlNavigation Select;
    public event MyEventHandlerCtlNavigation Modify;
    public event MyEventHandlerCtlNavigation Eliminate;

    private string profile = "";
    private string role = "";

    private DataTable dtRequirements
    {
        get
        {
            return (DataTable)(Session["dtRequirements"]);
        }
        set
        {
            (Session["dtRequirements"]) = value;
        }
    }

    public bool Enabled
    {
        get
        {
            return (ctlComboBox.Enabled || btnConsult.Enabled || ctlGridView.Enabled);
        }
        set
        {
            ctlComboBox.Enabled = value;
            btnConsult.Enabled = value;
            ctlGridView.Enabled = value;
        }
    }

    private int iIdFatherNav
    {
        get
        {
            return (int)(Session["iIdFatherNav"]);
        }
        set
        {
            Session["iIdFatherNav"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        // Cargar combobox

        HttpCookie cookie = Request.Cookies.Get("requmanaLogin");
        if (cookie == null)
        {
            Response.Redirect("FrmLogin.aspx");
        }
        else
        {
            profile = cookie.Values[3];
            role = cookie.Values[4];
        }

        this.Enabled = true;

        if (!IsPostBack)
        {
            CtrlProject ctrlProject = new CtrlProject();

            // Revisar la igualdad a cero, equivalente a administrador
            if (Int32.Parse(cookie.Values[3]) == 0)
            {
                ctlComboBox.setDataSource(ctrlProject.getData(), "Name", "Id");
            }
            else
            {
                ctlComboBox.setDataSource(ctrlProject.getDataC(Int32.Parse(cookie.Values[5])), "Name", "Id");
            }

            dtRequirements = new DataTable();

            dtRequirements.Columns.Add("Id", Type.GetType("System.String"));
            dtRequirements.Columns.Add("Name", Type.GetType("System.String"));
        }

        ctlGridView.DataSource = dtRequirements;
        
        if (!IsPostBack)
        {
            ctlGridView.DataBind();
            iIdFatherNav = 0;
        }

    }

    protected void GridView_RowCommand(object source, GridViewCommandEventArgs e)
    {
        int index = Convert.ToInt32(e.CommandArgument);
        GridViewRow selectedRow = ctlGridView.Rows[index];
        TableCell cellId = selectedRow.Cells[0];

        if (e.CommandName == "Select")
        {
            if (Select != null)
            {
                iIdFatherNav = int.Parse(cellId.Text);
                updateReqGrid(Int32.Parse(ctlComboBox.getSelectedId()));
                Select(this, int.Parse(cellId.Text));
            }
        }

        if (e.CommandName == "Modify")
        {
            if (Modify != null)
            {
                Modify(this, int.Parse(cellId.Text));
            }
            ctlGridView.DataBind();
        }

        if (e.CommandName == "Eliminate")
        {
            if (Eliminate != null)
            {
                Eliminate(this, int.Parse(cellId.Text));
            }
            ctlGridView.DataBind();
        }
    }

    public void clearRequirements()
    {
        dtRequirements.Clear();
    }

    protected void btnConsult_Click(object sender, EventArgs e)
    {
        updateReqGrid(Int32.Parse(ctlComboBox.getSelectedId()));
    }

    private void updateReqGrid(int idProject)
    {
        CtrlFunctionalRequirement ctrlFunctionalRequirement = new CtrlFunctionalRequirement();

        setDataSource(ctrlFunctionalRequirement.getFilterData(idProject, iIdFatherNav));
    }

    
    public void setDataSource(DataSet dataSet)
    {
        ctlGridView.DataSource = dataSet;
        if (!IsPostBack)
        {
            ctlGridView.DataBind();
        }
        ctlGridView.DataBind();
    }

}
